package com.imm.da.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.ImageViewTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2021 2021/9/12 12:39
 */
object ImgLoader {

    /**
     * 验证上下文是否可用
     */
    private fun usability(view : View?) : Boolean{
        if (view == null || view.context == null){
            return true
        }
        if (view.context is Activity && (view.context as Activity).isDestroyed){
            return true
        }
        return false
    }

    /**
     * 显示目标 ImageView，回调 Drawable
     */
    fun load(
        view: ImageView,
        url: String,
        placeholderId: Int? = 0,
        errorId: Int? = 0,
        skipMemory: Boolean = false,
        strategy: DiskCacheStrategy = DiskCacheStrategy.RESOURCE,
        target: ImageViewTarget<Drawable> = createImgTarget(view)
    ){
        if (usability(view)){
            return
        }
        Glide
            .with(view)
            .load(url)
            .apply(options(placeholderId, errorId, skipMemory, strategy))
            .into(target)
    }

    private fun createImgTarget(view: ImageView) = object : ImageViewTarget<Drawable>(view) {
        override fun setResource(resource: Drawable?) {
            getView().setImageDrawable(resource)
        }
    }

    /**
     * 显示目标 View，回调 Drawable
     */
    fun load(
        layout: View,
        url: String,
        placeholderId: Int? = 0,
        errorId: Int? = 0,
        skipMemory: Boolean = false,
        strategy: DiskCacheStrategy = DiskCacheStrategy.RESOURCE,
        target: Target<Drawable> = createTarget(layout)
    ){
        if (usability(layout)){
            return
        }
        Glide
            .with(layout)
            .load(url)
            .apply(options(placeholderId, errorId, skipMemory, strategy))
            .into(target)
    }

    private fun createTarget(layout: View) = object : CustomViewTarget<View, Drawable>(layout) {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            Log.v("aaaaa", "util onResourceReady resource:$resource")
            getView().background = resource
        }

        override fun onLoadFailed(errorDrawable: Drawable?) {
            Log.v("aaaaa", "util onLoadFailed errorDrawable:$errorDrawable")
            getView().background = errorDrawable
        }

        override fun onResourceCleared(placeholder: Drawable?) {
            Log.v("aaaaa", "util onResourceCleared placeholder:$placeholder")
            getView().background = placeholder
        }

    }

    /**
     * 图片自定义属性
     * RequestOptions.override(100, 100) 宽高剪裁
     * RequestOptions.circleCrop() 圆形图 或者 RequestOptions.circleCropTransform()
     *
     */
    @SuppressLint("CheckResult")
    private fun options(
        placeholderId: Int?,
        errorId: Int?,
        skipMemory: Boolean = false,
        strategy: DiskCacheStrategy = DiskCacheStrategy.RESOURCE
    ) : RequestOptions {
        val options = RequestOptions()
        if (placeholderId != null && placeholderId > 0){
            options.placeholder(placeholderId)
        }
        if (errorId != null && errorId > 0){
            options.error(errorId)
        }

        options.centerCrop()
            // 缓存转换后的图片
            .diskCacheStrategy(strategy)
            .skipMemoryCache(skipMemory)

        // 这里可以配置圆角/圆形/高斯模糊等

        return options
    }

}